package com.popcorn.comment.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.comment.dao.commentDAO;
import com.popcorn.comment.model.commentModel;
@WebServlet(name = "commentController",urlPatterns = "*.jo")
public class commentController extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
	      response.setContentType("text/html; charset=UTF-8");
	      response.setCharacterEncoding("UTF-8");
	      request.setCharacterEncoding("UTF-8");
	      
	      String RequestURI = request.getRequestURI(); 
	      String contextPath = request.getContextPath();   
	      String command = RequestURI.substring(contextPath.length());
	      
	      System.out.println("========================commentController======================== ");
	      System.out.println("RequestURI : "+ RequestURI);
	      System.out.println("contextPath : "+ contextPath);
	      System.out.println("command : "+ command);
		
        //1. 요청분석
        String cmd = request.getParameter("cmd");
        
        if(cmd.equals("insert")){
        	System.out.println("insert");
            insert(request,response);
        }else if(cmd.equals("list")){
        	System.out.println("list");
        	list(request,response);
        }else if(cmd.equals("delete")){
        	System.out.println("delete");
        	delete(request,response);
        }
    }
	
	
    private void list(HttpServletRequest request, HttpServletResponse response)
    		throws ServletException, IOException {
    	String COMMAND = request.getParameter("COMMAND");
    	List list = new ArrayList();
    	commentDAO dao=new commentDAO();
    	list = dao.getList(COMMAND);
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        pw.println("<result>");
        if(list.size()!=0){
        	pw.println("<info>success</info>"); 
        	commentModel model;
        	for(int i=0 ; i< list.size(); i++){
        		model = (commentModel) list.get(i);
	        	pw.println("<nickname>"+model.getNICKNAME()+"</nickname>");
	        	pw.println("<date>"+model.getCOM_DTM()+"</date>");
	        	pw.println("<content>"+model.getCONTENT()+"</content>");
	        	pw.println("<com_seq>"+String.valueOf(model.getCOM_SEQ())+"</com_seq>");
        	}
        }else{
            pw.println("<info>fail</info>");
        }
        pw.println("</result>");
        pw.close();
	}
	private void insert(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //사용자가 보낸 아이디와 영화평 얻어오기.
        String NICKNAME=request.getParameter("NICKNAME");
        String COMMAND = request.getParameter("COMMAND");
        String comments=request.getParameter("comments");
        
        System.out.println("=============댓글 추가=============");
        System.out.println(NICKNAME);
        System.out.println(COMMAND);
        System.out.println(comments);
        
        //정보를 vo 객체에 담기.
        commentModel model=new commentModel();
        model.setCONTENT(comments);
        model.setNICKNAME(NICKNAME);
        model.setSEQ_BBS(COMMAND);
        //2. 요청에 따른 비즈니스로직을 처리하기 위한 자바객체 호출
        commentDAO dao=new commentDAO();
        
        //DB에 vo객체 추가하기.
        boolean n=dao.insert(model);
        //3. 결과값을 xml로 응답하기 (ajax는 페이지이동하면 안된다.)
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        pw.println("<result>");
        if(n==true){
            pw.println("<info>success</info>");
        }else{
            pw.println("<info>fail</info>");
        }
        pw.println("</result>");
        pw.close();
    }
	
	private void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //사용자가 보낸 아이디와 영화평 얻어오기.
        String idx=request.getParameter("idx");
        String NICKNAME=request.getParameter("NICKNAME");
        //정보를 vo 객체에 담기.
        //2. 요청에 따른 비즈니스로직을 처리하기 위한 자바객체 호출
        commentDAO dao=new commentDAO();
        
        //DB에 vo객체 추가하기.
        boolean n=dao.delete(idx,NICKNAME);
        //3. 결과값을 xml로 응답하기 (ajax는 페이지이동하면 안된다.)
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        pw.println("<result>");
        if(n==true){
            pw.println("<info>success</info>");
        }else{
            pw.println("<info>fail</info>");
        }
        pw.println("</result>");
        pw.close();
    }
}